package com.example.smartmirror2;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.ai.tts.TtsClient;
import ohos.ai.tts.TtsListener;
import ohos.ai.tts.TtsParams;
import ohos.ai.tts.constants.TtsEvent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.*;
import ohos.utils.PacMap;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class ReportServiceAbility extends Ability {
//    语音播报java
    private boolean initItsResult;
    private static final TtsListener ttsListener = new TtsListener() {
        @Override
        public void onEvent(int eventType, PacMap pacMap) {
            // Log.info("onEvent:" + eventType);
            if (eventType == TtsEvent.CREATE_TTS_CLIENT_SUCCESS) {
                // Log.info("TTS Client create success");
            }
        }

        @Override
        public void onStart(String utteranceId) {
            // Log.info(utteranceId + " audio synthesis begins");
        }

        @Override
        public void onProgress(String utteranceId, byte[] audioData, int progress) {
            // Log.info(utteranceId + " audio synthesis progress：" + progress);
        }

        @Override
        public void onFinish(String utteranceId) {
            // Log.info(utteranceId + " audio synthesis completed");
        }

        @Override
        public void onSpeechStart(String utteranceId) {
            // Log.info(utteranceId + " begins to speech");
        }

        @Override
        public void onSpeechProgressChanged(String utteranceId, int progress) {
            // Log.info(utteranceId + " speech progress：" + progress);
        }

        @Override
        public void onSpeechFinish(String utteranceId) {
            // Log.info(utteranceId + " speech completed");
        }

        @Override
        public void onError(String utteranceId, String errorMessage) {
            // Log.info(utteranceId + " errorMessage: " + errorMessage);
        }
    };
    private void initTtsEngine() {
        TtsClient.getInstance().create(this, ttsListener);
    }
    public void onEvent() {
//        HiLog.info(LABEL_LOG, "onEvent...");
        // 定义TTS客户端创建成功的回调函数
            TtsParams ttsParams = new TtsParams();
            ttsParams.setDeviceId(UUID.randomUUID().toString());
            initItsResult = TtsClient.getInstance().init(ttsParams);

    }
    private void readText(String str) {
        if (initItsResult) {
//            HiLog.info(LABEL_LOG, "initItsResult is true, speakText");
            TtsClient.getInstance().speakText(str, null);
            System.out.println("----------------播报");
        } else {
//            HiLog.error(LABEL_LOG, "initItsResult is false");
        }
    }

    //    js FA调用java PA
    MyRemote myRemote = new MyRemote();

    @Override
    protected IRemoteObject onConnect(Intent intent) {

        //返回RemoteObject的对象
        return myRemote.asObject();
    }

    class MyRemote extends RemoteObject implements IRemoteBroker {

        public MyRemote() {
            super("descriptor");
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
            switch (code) {
                case 99:
                    String str = data.readString();
//                    HiLog.info(LABEL_LOG, "str:"+str);
                    System.out.println("report调用javaPA");
                    initTtsEngine();
                    onEvent();
                    readText(str);
                    //添加返回数据
                    Map<String, Object> result = new HashMap<String, Object>();
/*                    result.put("id", "002");
                    result.put("name", "xiaohuang");*/
                    reply.writeString(ZSONObject.toZSONString(result));
                    break;
                default:
                    return false;
            }
            return true;
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }
    }


}
